<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------

namespace api\api\controller;

use think\Db;
use think\facade\Validate;
use cmf\controller\RestBaseController;
use thinkexceptionHttpException;
use   thinkacadeRequest;

class PublicController extends RestBaseController
{
    public function get_yingxiao()
    {
//       $data= cmf_api_request('http://yingxiao.sdhxtech.com/api');
        $data = cmf_curl_get('http://yingxiao.sdhxtech.com/api');
        $data = json_decode($data);
//        var_dump($data);
//        var_dump($data->code);
        if (isset($data)) {
            if ($data->code == 1) {
                foreach ($data->data as $k => $v) {
                    var_dump($v);
                    exit();
                }
            }
        }

    }

    public function get_slider_list()
    {
        $where = array(
            'slide_id' => 1,
            'status' => 1
        );
        $slider_list = Db::name('slide_item')->where($where)->order('list_order desc')->select();
        if ($slider_list->isEmpty()) {
            $this->error("加载完成");
        }
        $slider_data = array();
        foreach ($slider_list as $k => $v) {
            // $slider_data[$k] = $v;
            // $slider_data[$k]['image'] = cmf_get_asset_url($v['image']);
            $slider_data[$k] = cmf_get_asset_url($v['image']);
        }
        $this->success("获取成功!", $slider_data);
    }


    public function get_nav_list()
    {
        $nav_list = Db::name('portal_category')->where(array('status' => 1, 'parent_id' => 0))->order('list_order desc')->select();
        if ($nav_list->isEmpty()) {
            $this->error("参数错误");
        } else {

            $backarr['all'] = $nav_list;
            $shuxing = [];
            foreach ($nav_list as $k => $val) {
                $shuxing[$k]['title'] = $val['name'];
                $shuxing[$k]['bg'] = '#FFF';
                $shuxing[$k]['bgSelected'] = '#FFF';

            }
            $backarr['shuxing'] = $shuxing;
            $this->success("获取成功!", $backarr);

        }
    }

    public function get_portal_list($nav_select_id, $page)
    {
        $res = Db::name('portal_category_post')
            ->field('b.*')
            ->alias('a')
            ->join('__PORTAL_POST__ b', 'a.post_id = b.id')
            ->where('b.delete_time', 0)
            ->where(array('a.category_id' => $nav_select_id))
            ->order('is_top desc,id desc')
            ->page($page, 10)
            ->select();
        $back_arr = [];
        if (!empty($res)) {
            foreach ($res as $k => $v) {
                $back_arr[$k] = $v;
                $back_arr[$k]['create_time'] = date('Y-m-d h:i:s', $v['create_time']);
                $back_arr[$k]['thumbnail'] = cmf_get_image_url($v['thumbnail']);
            }
        }
        $this->success("获取成功!", $back_arr);
    }

    public function get_post_info()
    {
        $post_id = input('post_id');
        $res = DB::name('portal_post')->where('id=' . $post_id)->find();
        if (empty($res)) {
            $this->error('暂无');

        }
        DB::name('portal_post')->where('id=' . $post_id)->setInc('post_hits', 1);

        $res['post_content'] = htmlspecialchars_decode($res['post_content']);
        $res['create_time'] = date('H:i', $res['create_time']);
        $this->success('成功', $res);
    }

    /**
     * @api {post} get_zixun_types 获取分类栏目组新版  固定前三.
     * @apiGroup index
     *
     * @apiSuccessExample Success-Response:
     *     HTTP/1.1 200 OK
     *
     *  {
     *      "Code": 1,
     *   "msg": '更新成功'
     *      "data": ""
     *  }
     **/

    public function get_zixun_types()
    {

        $where = array('status' => 1);

        $zixun_types = Db::name('portal_category')->where($where)->order('list_order desc')->select()->toArray();
        if (count($zixun_types) < 1) {
            $this->error("参数错误");
        } else {

            $backarr = [];
            foreach ($zixun_types as $k => $val) {
                $backarr[$k] = $val;
                $backarr[$k]['current'] = true;
            }

            $this->success("获取成功!", $backarr);
        }

    }


    /**
     * @api {post} get_portal_list_v1 根据分类id 分页获取文章列表.
     * @apiGroup index
     *
     * @apiParam {Int} page 当前页数.
     * @apiParam {Int} category 分类id.
     * @apiSuccessExample Success-Response:
     *     HTTP/1.1 200 OK
     *
     *  {
     *      "Code": 1,
     *   "msg": '更新成功'
     *      "data": ""
     *  }
     **/

    public function get_portal_list_v1()
    {
        $page = input('page');
        $category = input('category');
        $where = array('post.category_id' => $category, 'post.status' => 1, 'portal.post_status' => 1);
        $portal = Db::name('portal_category_post')
            ->alias('post')
            ->join('__PORTAL_POST__ portal', 'post.post_id = portal.id')
            ->where($where)
            ->where('delete_time', 0)
            ->field('portal.id,portal.user_id,portal.post_title,portal.post_excerpt,portal.thumbnail,portal.post_source,portal.published_time,portal.post_content,portal.post_hits,portal.post_keywords')
            ->order('portal.published_time desc,  post.list_order  ')
            ->page($page, 8)//必须每次加载8条与前端广告插入对应
            // -> fetchSql(true)
            ->select()
            ->toArray();
        $back_arr = [];
        $back_arr['list'] = [];
        if (count($portal) > 0) {
            foreach ($portal as $k => $v) {
                $user_nickname = Db::name('user')->field('user_nickname')->where('id', $v['user_id'])->find();
                $portal[$k]['user_nickname'] = $user_nickname['user_nickname'];
                $portal[$k]['published_time'] = date('Y-m-d h:i:s', $v['published_time']);
                //获取编辑器中的图片
                $image_content = cmf_get_content_images(cmf_replace_content_file_url(htmlspecialchars_decode($v['post_content'])));
                if (count($image_content) > 2) {
                    //多图模式
                    $portal[$k]['mode'] = 'column';
                    $portal[$k]['images'] = $image_content;
                } else {
                    if (!empty($v['thumbnail'])) {
                        //单图模式
                        $portal[$k]['mode'] = 'image';
                        $portal[$k]['images'] = cmf_get_image_url($v['thumbnail']);
                    } else {
                        //单图模式
                        $portal[$k]['mode'] = 'base';

                    }


                }
                unset($portal[$k]['post_content']);
            }
            $back_arr['list'] = $portal;
        }

        $this->success("获取成功!", $back_arr);
    }

    /**
     * @api {post} get_portal_detail_v1 获取文章详情
     * @apiGroup index
     *
     * @apiParam {Int} id 文章id.
     * @apiSuccessExample Success-Response:
     *     HTTP/1.1 200 OK
     *
     *  {
     *      "Code": 1,
     *   "msg": '更新成功'
     *      "data": ""
     *  }
     **/

    public function get_portal_detail_v1()
    {
        $id = input('id');

        $where = array('id' => $id, 'post_status' => 1);
        $data = Db::name('portal_post')
            ->where($where)
            ->find();
        if (!empty($data)) {
            $data['post_content'] = cmf_replace_content_file_url(removeImgall(htmlspecialchars_decode($data['post_content'])));
            $data['more'] = $this->getMoreAttr($data['more']);
            $liwhere = array('id' => $data['user_id']);

            $findUser = Db::name("user")->where($liwhere)->find();
            $data['user_nickname'] = $findUser['user_nickname'];
            $data['avatar'] = cmf_get_image_url($findUser['avatar']);
            $data['published_time'] = date('Y-m-d h:i:s', $data['published_time']);
            $data['thumbnail'] = cmf_get_image_url($data['thumbnail']);
//            $data['messageimg'] = cmf_get_image_url($data['messageimg']);

            $back_arr['data'] = $data;


            $this->success("获取成功!", $back_arr);
        } else {
            $this->error("加载完成");
        }
    }

    /**
     * more 自动转化 自动转换url地址
     * @param $value
     * @return array
     */
    public function getMoreAttr($value)
    {
        $more = json_decode($value, true);
        if (!empty($more['thumbnail'])) {
            $more['thumbnail'] = cmf_get_image_url($more['thumbnail']);
        }

        if (!empty($more['audio'])) {
            $more['audio'] = cmf_get_file_download_url($more['audio']);
        }

        if (!empty($more['video'])) {
            $more['video'] = cmf_get_file_download_url($more['video']);
        }

        if (!empty($more['photos'])) {
            foreach ($more['photos'] as $key => $value) {
                $more['photos'][$key]['url'] = cmf_get_image_url($value['url']);
            }
        }

        if (!empty($more['files'])) {
            foreach ($more['files'] as $key => $value) {
                $more['files'][$key]['url'] = cmf_get_file_download_url($value['url']);
            }
        }
        return $more;
    }


    /**
     *  用户注册
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function register()
    {
        $validate = new \think\Validate([
            'username' => 'require',
            'password' => 'require',
            'verification_code' => 'require'
        ]);

        $validate->message([
            'username.require' => '请输入手机号,邮箱!',
            'password.require' => '请输入您的密码!',
            'verification_code.require' => '请输入数字验证码!'
        ]);

        $data = $this->request->param();
        if (!$validate->check($data)) {
            $this->error($validate->getError());
        }

        $user = [];

        $findUserWhere = [];

        if (Validate::is($data['username'], 'email')) {
            $user['user_email'] = $data['username'];
            $findUserWhere['user_email'] = $data['username'];
        } else if (cmf_check_mobile($data['username'])) {
            $user['mobile'] = $data['username'];
            $findUserWhere['mobile'] = $data['username'];
        } else {
            $this->error("请输入正确的手机或者邮箱格式!");
        }

//        $errMsg = cmf_check_verification_code($data['username'], $data['verification_code']);
//        if (!empty($errMsg)) {
//            $this->error($errMsg);
//        }

        $findUserCount = Db::name("user")->where($findUserWhere)->count();

        if ($findUserCount > 0) {
            $this->error("此账号已存在!");
        }
        $r = rand(100000, 999999);
        $user['user_nickname'] = '建光出行' . $r;
        $user['create_time'] = time();
        $user['user_status'] = 1;
        $user['user_type'] = 2;
        $user['user_pass'] = cmf_password($data['password']);
        $user['avatar'] = 'avatar.png';

        $result = Db::name("user")->insert($user);


        if (empty($result)) {
            $this->error("注册失败,请重试!");
        }

        $this->success("注册并激活成功,请登录!");

    }

    /**
     * 用户登录
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    // TODO 增加最后登录信息记录,如 ip
    public function login()
    {
        $validate = new \think\Validate([
            'username' => 'require',
            'password' => 'require'
        ]);
        $validate->message([
            'username.require' => '请输入手机号,邮箱或用户名!',
            'password.require' => '请输入您的密码!'
        ]);

        $data = $this->request->param();
        if (!$validate->check($data)) {
            $this->error($validate->getError());
        }

        $findUserWhere = [];

        if (Validate::is($data['username'], 'email')) {
            $findUserWhere['user_email'] = $data['username'];
        } else if (cmf_check_mobile($data['username'])) {
            $findUserWhere['mobile'] = $data['username'];
        } else {
            $findUserWhere['user_login'] = $data['username'];
        }

        $findUser = Db::name("user")->where($findUserWhere)->find();

        if (empty($findUser)) {
            $this->error("用户不存在!");
        } else {

            switch ($findUser['user_status']) {
                case 0:
                    $this->error('您已被拉黑!');
                case 2:
                    $this->error('账户还没有验证成功!');
            }

            if (!cmf_compare_password($data['password'], $findUser['user_pass'])) {
                $this->error("密码不正确!");
            }
        }

        $allowedDeviceTypes = $this->allowedDeviceTypes;

        if (empty($this->deviceType) && (empty($data['device_type']) || !in_array($data['device_type'], $this->allowedDeviceTypes))) {
            $this->error("请求错误,未知设备!");
        } else if (!empty($data['device_type'])) {
            $this->deviceType = $data['device_type'];
        }

//        Db::name("user_token")
//            ->where('user_id', $findUser['id'])
//            ->where('device_type', $data['device_type']);
        $findUserToken = Db::name("user_token")
            ->where('user_id', $findUser['id'])
            ->where('device_type', $this->deviceType)
            ->find();
        $currentTime = time();
        $expireTime = $currentTime + 24 * 3600 * 180;
        $token = md5(uniqid()) . md5(uniqid());
        if (empty($findUserToken)) {
            $result = Db::name("user_token")->insert([
                'token' => $token,
                'user_id' => $findUser['id'],
                'expire_time' => $expireTime,
                'create_time' => $currentTime,
                'device_type' => $this->deviceType
            ]);
        } else {
            $result = Db::name("user_token")
                ->where('user_id', $findUser['id'])
                ->where('device_type', $this->deviceType)
                ->update([
                    'token' => $token,
                    'expire_time' => $expireTime,
                    'create_time' => $currentTime
                ]);
        }


        if (empty($result)) {
            $this->error("登录失败!");
        }
        $findUser['avatar'] = cmf_get_image_url($findUser['avatar']);
        $this->success("登录成功!", ['token' => $token, 'user' => $findUser]);
    }

    /**
     * qq登录
     */
    public function qqlogin()
    {
        $data = $this->request->param();
//$data=$data['data'];
        $data = json_decode($data['data']);

        $findThirdPartyUser = Db::name("third_party_user")
            ->where('openid', $data->openid)
            ->where('app_id', $data->appid)
            ->find();

        $currentTime = time();
        $ip = $this->request->ip(0, true);
        if (empty($this->deviceType) && (empty($data->device_type) || !in_array($data->device_type, $this->allowedDeviceTypes))) {
            $this->error("请求错误,未知设备!");
        } else if (!empty($data->device_type)) {
            $this->deviceType = $data->device_type;
        }

        if ($findThirdPartyUser) {
            $userId = $findThirdPartyUser['user_id'];
            $token = cmf_generate_user_token($findThirdPartyUser['user_id'], $this->deviceType);

            $userData = [
                'last_login_ip' => $ip,
                'last_login_time' => $currentTime,
                'login_times' => Db::raw('login_times+1'),
                'more' => ''
            ];

            if (isset($data->unionId)) {
                $userData['union_id'] = $data->unionId;
            }

            Db::name("third_party_user")
                ->where('openid', $data->openid)
                ->where('app_id', $data->appid)
                ->update($userData);

        } else {

            //TODO 使用事务做用户注册
            $userId = Db::name("user")->insertGetId([
                'create_time' => $currentTime,
                'user_status' => 1,
                'user_type' => 2,
                'sex' => 2,
                'user_nickname' => $data->nickname,
                'avatar' => $data->figureurl_1,
                'last_login_ip' => $ip,
                'last_login_time' => $currentTime,
            ]);

            Db::name("third_party_user")->insert([
                'openid' => $data->openid,
                'user_id' => $userId,
                'third_party' => 'qq',
                'app_id' => $data->appid,
                'last_login_ip' => $ip,
                'union_id' => isset($data->unionId) ? $data->unionId : '',
                'last_login_time' => $currentTime,
                'create_time' => $currentTime,
                'login_times' => 1,
                'status' => 1,
                'more' => ''
            ]);

            $token = cmf_generate_user_token($userId, $this->deviceType);

        }

        $user = Db::name('user')->where('id', $userId)->find();
        $user['avatar'] = cmf_get_image_url($user['avatar']);
        $this->success("登录成功!", ['token' => $token, 'user' => $user]);


    }

    /**微信第三方登录
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function wxlogin()
    {


        $data = $this->request->param();
//$data=$data['data'];
        $data = json_decode($data['data']);
        $findThirdPartyUser = Db::name("third_party_user")
            ->where('openid', $data->openid)
            ->where('app_id', $data->appid)
            ->find();

        $currentTime = time();
        $ip = $this->request->ip(0, true);
        if (empty($this->deviceType) && (empty($data->device_type) || !in_array($data->device_type, $this->allowedDeviceTypes))) {
            $this->error("请求错误,未知设备!");
        } else if (!empty($data->device_type)) {
            $this->deviceType = $data->device_type;
        }

        if ($findThirdPartyUser) {
            $userId = $findThirdPartyUser['user_id'];
            $token = cmf_generate_user_token($findThirdPartyUser['user_id'], $this->deviceType);

            $userData = [
                'last_login_ip' => $ip,
                'last_login_time' => $currentTime,
                'login_times' => Db::raw('login_times+1'),
                'more' => ''
            ];

            if (isset($data->unionId)) {
                $userData['union_id'] = $data->unionId;
            }

            Db::name("third_party_user")
                ->where('openid', $data->openid)
                ->where('app_id', $data->appid)
                ->update($userData);

        } else {

            //TODO 使用事务做用户注册
            $userId = Db::name("user")->insertGetId([
                'create_time' => $currentTime,
                'user_status' => 1,
                'user_type' => 2,
                'sex' => $data->sex,
                'user_nickname' => $data->nickname,
                'avatar' => $data->headimgurl,
                'last_login_ip' => $ip,
                'last_login_time' => $currentTime,
            ]);

            Db::name("third_party_user")->insert([
                'openid' => $data->openid,
                'user_id' => $userId,
                'third_party' => '微信',
                'app_id' => $data->appid,
                'last_login_ip' => $ip,
                'union_id' => isset($data->unionId) ? $data->unionId : '',
                'last_login_time' => $currentTime,
                'create_time' => $currentTime,
                'login_times' => 1,
                'status' => 1,
                'more' => ''
            ]);

            $token = cmf_generate_user_token($userId, $this->deviceType);

        }

        $user = Db::name('user')->where('id', $userId)->find();
        $user['avatar'] = cmf_get_image_url($user['avatar']);
        $this->success("登录成功!", ['token' => $token, 'user' => $user]);


    }

    /**手机号登录
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function mobilelogin()
    {
        $validate = new \think\Validate([
            'username' => 'require',
        ]);
        $validate->message([
            'username.require' => '请输入手机号!',
        ]);

        $data = $this->request->param();
        if (!$validate->check($data)) {
            $this->error($validate->getError());
        }

        $findUserWhere = [];

        if (Validate::is($data['username'], 'email')) {
            $findUserWhere['user_email'] = $data['username'];
        } else if (cmf_check_mobile($data['username'])) {
            $findUserWhere['mobile'] = $data['username'];
        } else {
            $this->error("请输入正确的手机号!");
        }

        $findUser = Db::name("user")->where($findUserWhere)->find();

        if (empty($findUser)) {
            $this->error("用户不存在!");
        } else {

            switch ($findUser['user_status']) {
                case 0:
                    $this->error('您已被拉黑!');
                case 2:
                    $this->error('账户还没有验证成功!');
            }

//            $errMsg = cmf_check_verification_code($data['username'], $data['verification_code']);
//        if (!empty($errMsg)) {
//            $this->error($errMsg);
//        }
        }

        $allowedDeviceTypes = $this->allowedDeviceTypes;

        if (empty($this->deviceType) && (empty($data['device_type']) || !in_array($data['device_type'], $this->allowedDeviceTypes))) {
            $this->error("请求错误,未知设备!");
        } else if (!empty($data['device_type'])) {
            $this->deviceType = $data['device_type'];
        }

//        Db::name("user_token")
//            ->where('user_id', $findUser['id'])
//            ->where('device_type', $data['device_type']);
        $findUserToken = Db::name("user_token")
            ->where('user_id', $findUser['id'])
            ->where('device_type', $this->deviceType)
            ->find();
        $currentTime = time();
        $expireTime = $currentTime + 24 * 3600 * 180;
        $token = md5(uniqid()) . md5(uniqid());
        if (empty($findUserToken)) {
            $result = Db::name("user_token")->insert([
                'token' => $token,
                'user_id' => $findUser['id'],
                'expire_time' => $expireTime,
                'create_time' => $currentTime,
                'device_type' => $this->deviceType
            ]);
        } else {
            $result = Db::name("user_token")
                ->where('user_id', $findUser['id'])
                ->where('device_type', $this->deviceType)
                ->update([
                    'token' => $token,
                    'expire_time' => $expireTime,
                    'create_time' => $currentTime
                ]);
        }


        if (empty($result)) {
            $this->error("登录失败!");
        }
        $findUser['avatar'] = cmf_get_image_url($findUser['avatar']);
        $this->success("登录成功!", ['token' => $token, 'user' => $findUser]);
    }

    /**
     * 用户退出
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function logout()
    {
        $userId = $this->getUserId();
        Db::name('user_token')->where([
            'token' => $this->token,
            'user_id' => $userId,
            'device_type' => $this->deviceType
        ])->update(['token' => '']);

        $this->success("退出成功!");
    }

    /**
     * 用户密码重置
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function passwordReset()
    {
        $validate = new \think\Validate([
            'username' => 'require',
            'password' => 'require',
            'verification_code' => 'require'
        ]);

        $validate->message([
            'username.require' => '请输入手机号,邮箱!',
            'password.require' => '请输入您的密码!',
            'verification_code.require' => '请输入数字验证码!'
        ]);

        $data = $this->request->param();
        if (!$validate->check($data)) {
            $this->error($validate->getError());
        }

        $userWhere = [];
        if (Validate::is($data['username'], 'email')) {
            $userWhere['user_email'] = $data['username'];
        } else if (cmf_check_mobile($data['username'])) {
            $userWhere['mobile'] = $data['username'];
        } else {
            $this->error("请输入正确的手机或者邮箱格式!");
        }

        $errMsg = cmf_check_verification_code($data['username'], $data['verification_code']);
        if (!empty($errMsg)) {
            $this->error($errMsg);
        }

        $userPass = cmf_password($data['password']);
        Db::name("user")->where($userWhere)->update(['user_pass' => $userPass]);

        $this->success("密码重置成功,请使用新密码登录!");

    }

    /**
     * 用户手机号码更换
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function mobileReset()
    {
        $validate = new \think\Validate([
            'username' => 'require',
            'newmobile' => 'require',
            'verification_code' => 'require'
        ]);

        $validate->message([
            'username.require' => '请输入手机号,邮箱!',
            'newmobile.require' => '请输入您的新手机号!',
            'verification_code.require' => '请输入数字验证码!'
        ]);

        $data = $this->request->param();
        if (!$validate->check($data)) {
            $this->error($validate->getError());
        }

        $userWhere = [];
        if (Validate::is($data['username'], 'email')) {
            $userWhere['user_email'] = $data['username'];
        } else if (cmf_check_mobile($data['username'])) {
            // $userWhere['mobile'] = $data['username'];
        } else if (cmf_check_mobile($data['newmobile'])) {
            $userWhere['mobile'] = $data['newmobile'];
        } else {
            $this->error("请输入正确的手机或者邮箱格式!");
        }

        $errMsg = cmf_check_verification_code($data['username'], $data['verification_code']);
        if (!empty($errMsg)) {
            $this->error($errMsg);
        }

        $Where['mobile'] = $data['username'];
        $findUser = Db::name("user")->where($Where)->find();

        if (empty($findUser)) {
            $this->error("用户不存在!");
        } else {

            switch ($findUser['user_status']) {
                case 0:
                    $this->error('您已被拉黑!');
                case 2:
                    $this->error('账户还没有验证成功!');
            }
            $findUsernew = Db::name("user")->where($userWhere)->find();
            if ($findUsernew) {
                $this->error("新手机号已存在!");
            }
        }


        Db::name("user")->where($userWhere)->update(['mobile' => $data['newmobile']]);

        $this->success("手机号重置成功!");

    }

    /**
     * @api {post} upload_oneimg 图片上传.
     * @apiGroup user
     *
     * @apiParam {Int} token 用户token.
     *
     * @apiSuccess {String} code 返回值.
     * @apiSuccess {String} msg  返回说明.
     * @apiSuccess {String} data  返回数据.
     *
     * @apiSuccessExample Success-Response:
     *     HTTP/1.1 200 OK
     *     {
     *           "code": 1,
     *           "msg": '获取成功'
     *         "data": ""
     *     }
     *
     * @apiError {String} code 返回值.
     * @apiError {String} msg  返回说明.
     *
     * @apiErrorExample Error-Response:
     *     HTTP/1.1 404 Not Found
     *     {
     *         "code": 0,
     *           "msg": '获取失败'
     *     }
     */
    public function upload_oneimg()
    {
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('file');
        // 移动到框架应用根目录/public/upload/ 目录下
        $info = $file->move(WEB_ROOT . 'upload');
        if ($info) {
            $path = $info->getSaveName();
            if (empty($path)) {
                $this->error("上传失败!");
            }
            $path = str_replace("\\", "/", $path);
            $real_path = cmf_get_image_url($path);
            $this->success("上传成功", $real_path);
        } else {
            $this->error("上传失败!");
        }
    }
}
